package com.t.bricks.bussiness.service.userandopenid.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.t.bricks.bussiness.db.dao.userandopenid.UserAndOpenIdDao;
import com.t.bricks.bussiness.db.entity.userandopenid.UserAndOpenId;
import com.t.bricks.bussiness.service.userandopenid.UserAndOpenIdService;
import com.t.bricks.model.MsgEmity;

/**
 * 用户与关注号关联表'UserAndOpenId表'基本业务操作类接口实现
 */
@Service
public class UserAndOpenIdServiceImpl implements UserAndOpenIdService {

	@Autowired
	private UserAndOpenIdDao userAndOpenIdDao;
	
	/**
	 * 根据关注号查询数据
	 * @Param sOpenId
	 * @return UserAndOpenId
	 */
	@Override
	public MsgEmity findByOpenId(String sOpenId) {
		if (null == sOpenId || "".equals(sOpenId.trim())) {
			return new MsgEmity(false, "关注号不能为空！", 8001);
		}
		
		return userAndOpenIdDao.findByOpenId(sOpenId);
	}

	/**
	 * 微信用户登录
	 * @param sNameOrNo 用户的名称或工号
	 * @param sPass 用户密码
	 * @param sOpenId 用户的关注号
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity login(String sNameOrNo, String sPass, String sOpenId) {
		if (null == sNameOrNo || "".equals(sNameOrNo.trim())) {
			return MsgEmity.err(8011, "登录名为空");
		}
		
		if (null == sPass || "".equals(sPass.trim())) {
			return MsgEmity.err(8012, "密码为空");
		}
		
		if (null == sOpenId || "".equals(sOpenId.trim())) {
			return MsgEmity.err(8013, "关注号为空");
		}
		
		MsgEmity me = userAndOpenIdDao.getUserId(sNameOrNo, sPass);
		if (!me.isSuccess()) {
			return me;
		}
		
		String sId = me.getData();
		UserAndOpenId userAndOpenId = new UserAndOpenId();
		userAndOpenId.setsId(sId);
		userAndOpenId.setsNameOrNo(sNameOrNo);
		userAndOpenId.setsPass(sPass);
		userAndOpenId.setsOpenId(sOpenId.trim());
		userAndOpenId.setdCreateDate(new Date());
		
		me = userAndOpenIdDao.cover(userAndOpenId);
		if (!me.isSuccess()) {
			return me;
		}
		
		return MsgEmity.success("登录成功");
	}

}
